package com.xxyuan.batch._04_param_incr;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.util.Map;

@SpringBootApplication
@EnableBatchProcessing
public class IncrementParamJob {
    /**
     * job调度器
     */
    @Autowired
    private JobLauncher jobLauncher;

    /***
     * job构造器工厂
     */
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    /**
     * step构造器工厂
     */
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    /**
     * 构造一个step对象执行的任务(逻辑对象)
     */
    @Bean
    public Tasklet tasklet() {
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                Map<String, Object> jobParameters = chunkContext.getStepContext().getJobParameters();
                //要执行的逻辑-step1步骤执行逻辑
                System.out.println("================================================================");
                System.out.println("param-daily:"+jobParameters.get("daily"));
                System.out.println("================================================================");
                return RepeatStatus.FINISHED;
            }
        };
    }

    @Bean
    public DailyTimestampParamIncrementer dailyTimestampParamIncrementer(){
        return new DailyTimestampParamIncrementer();
    }

    /**
     * 构造一个step对象
     */
    @Bean
    public Step step1() {
        //tasklet step 执行逻辑，类似 Thread()--->可以执行runable接口
        return stepBuilderFactory.get("step1").tasklet(tasklet())
                .build();
    }



    /**
     * run.id自增
     */
//   @Bean
//    public Job job() {
//        return jobBuilderFactory.get("param-value-job")
//                .start(step1())
//                .incrementer(new RunIdIncrementer())
//                .build();
//    }


    /**
     *  时间戳增量器
     */
    @Bean
    public Job job() {
        return jobBuilderFactory.get("param-increment-job")
                .start(step1())
                .incrementer(dailyTimestampParamIncrementer())
                .build();
    }

    public static void main(String[] args) {
        /**
         * Job Instance  =  Job名称  +  识别参数
         *
         * Job Instance 一次执行创建一个 Job Execution对象
         *
         * 完整的一次Job Instance 执行可能创建一个Job Execution对象，也可能创建多个Job  Execution对象
         */
         SpringApplication.run(IncrementParamJob.class,args);
    }
}